#include<stdlib.h>
#include<stdio.h>
#include <string.h>
#include <mysql.h>
#define NUM 5

struct node {
	int val;
	int childcount;
	char attrval;
	struct node *children[NUM];
};
typedef struct node tree;

struct node *newnode(char attr)
{
	tree *newnode = malloc(sizeof(tree));
	newnode->childcount = NUM;
	newnode->attrval = attr;
	for (int i = 0; i < NUM; i++)
		newnode->children[i] = NULL;
	return (newnode);
}

tree *insert(tree * treenode, char *data)
{
	int i;
	int len = strlen(data);
	if (treenode == NULL) {
		treenode = newnode('X');
		printf("childcount of node=%d\n", treenode->childcount);
//printf("attribute of root val %d\n",treenode->attrval);
	}
	tree *currentnode = treenode;
	for (i = 0; i < len; i++) {
		char atrbt = data[i];
		int flag = 0;
//printf("childcount=%d\n",currentnode->childcount);
//printf("attribute inpput%c\n",data[i]);
		for (int h = 0; h < currentnode->childcount; h++) {
			if (currentnode->children[h]
			    && currentnode->children[h]->attrval == atrbt)
				flag = 1;
		}
		if (flag == 1) {
			for (int k = 0; k < currentnode->childcount; k++) {
				if (currentnode->children[k])
					if (currentnode->children[k]->attrval ==
					    atrbt) {
						currentnode =
						    currentnode->children[k];
						break;
					}
			}
		} else {
			int j = 0;
			while (currentnode->children[j])
				j++;
			currentnode->children[j] = newnode(atrbt);
			currentnode->children[j]->attrval = atrbt;
			currentnode = currentnode->children[j];
			printf("attrubute value=%c\n", currentnode->attrval);
		}
	}
	return (treenode);
}

void printnode(tree * printree)
{
	printf("%c\n", printree->attrval);
	for (int i = 0; i < printree->childcount; i++) {
		if (printree->children[i])
			printnode(printree->children[i]);
	}
}

void main()
{

	MYSQL_RES *res;
	MYSQL_ROW row;
	MYSQL *conn;

	struct node *root = NULL;
/*conn=dbconnection();

   if (mysql_query(conn, "select * from binary_table")) {
      printf("%s\n", mysql_error(conn));
      exit(1);
   }

   res = mysql_use_result(conn);
int num=0;

while ((row = mysql_fetch_row(res)) != NULL){
for(int i=0;i<3;i++) {
      printf("%d\n",atoi(row[i]));
}
}
*/
	root = insert(root, "aab");
	root = insert(root, "ebcda");
	root = insert(root, "eabca");
/*root=insert(root,"000");
root=insert(root,"001");
root=insert(root,"010");
root=insert(root,"100");
root=insert(root,"101");
root=insert(root,"110");
root=insert(root,"111");
root=insert(root,"101");
*/
//printf("after returning root\n");
//printf("root childcount:%d\n",root->childcount);
//printf("root attribute val:%d\n",root->attrval);
//printf("child 1 attribute%d\n",root->children[0]->attrval);
//printf("child 2 attribute%d\n",root->children[1]->attrval);

	printnode(root);

	return;
}
